/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rhdtp.demopf.control.admin;

import com.rhdtp.demopf.model.dao.DirectorDAO;
import com.rhdtp.demopf.model.dto.Director;
import com.rhdtp.demopf.security.UserSession;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import org.apache.log4j.Logger;
import org.primefaces.context.RequestContext;

/**
 *
 * @author rhdtp
 */
public class CatalogoDirector implements Serializable {

    private static final Logger logger = Logger.getLogger(CatalogoDirector.class);
    private Director director;
    private DirectorDAO directorDAO;
    private UserSession userSession;
    private List<Director> directorsFilter;

    public CatalogoDirector() {
    }

    public void save() {

        if (director.getId() != 0) {
            directorDAO.update(director);
            msgInfo("Info.", "El Director: " + director.getNombre() + " ah sido actualizado correctamente.");
        } else {
            director.setId(directorDAO.countId() + 1);
            directorDAO.save(director);
            msgInfo("Info.", "El Director: " + director.getNombre() + " ah sido guardado correctamente.");
        }

        init();
    }

    public void edit(Director ac) {
        director = directorDAO.load(ac);
        RequestContext.getCurrentInstance().scrollTo("registro:formulario");
    }

    public void remove(Director a) {
        if (directorDAO.delete(a)) {
            msgInfo("Info.", "El Director: " + a.getNombre() + " a sido borrado correctamente.");
        } else {
            msgError("Error", "No se pudo borrar el Director, esto puede ser porque tiene algún registro dependiente");
        }
    }

    public void resetForm() {
        init();
        RequestContext.getCurrentInstance().reset("registro:formulario");
    }

    @PostConstruct
    public void init() {
        director = new Director();
        directorsFilter = null;
    }

    public void msgInfo(String summary, String detail) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, summary, detail);
        FacesContext.getCurrentInstance().addMessage(null, message);
    }

    public void msgError(String summary, String detail) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, summary, detail);
        FacesContext.getCurrentInstance().addMessage(null, message);
    }

    public void msgWarning(String summary, String detail) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_WARN, summary, detail);
        FacesContext.getCurrentInstance().addMessage(null, message);
    }

    public List<Director> getDirectors() {
        return directorDAO.list();
    }

    public Director getDirector() {
        return director;
    }

    public void setDirector(Director director) {
        this.director = director;
    }

    public void setDirectorDAO(DirectorDAO directorDAO) {
        this.directorDAO = directorDAO;
    }

    public UserSession getUserSession() {
        return userSession;
    }

    public void setUserSession(UserSession userSession) {
        this.userSession = userSession;
    }

    public List<Director> getDirectorsFilter() {
        return directorsFilter;
    }

    public void setDirectorsFilter(List<Director> directorsFilter) {
        this.directorsFilter = directorsFilter;
    }
}
